<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-3-30
 * Time: 下午2:25
 */

namespace api\modules\chedai\controllers;


use api\modules\chedai\components\OnlineVerify;
use common\components\tools\Api;
use common\components\tools\Tools;
use common\models\AliFaceValidate;
use common\models\credit_app\Order;
use common\models\credit_app\OrderCert;
use common\models\online_verify_tables\ResInterview;
use common\models\Unionpay3002SetLog;
use wodrow\yii\rest\Controller;
use yii\base\ErrorException;
use yii\httpclient\Client;

class OnlineVerifyController extends Controller
{
    /**
     * 实时面签配置
     * @desc get/post
     */
    public function actionGetConfig()
    {
        $o = [];
        $o['api_sdk_url'] = OnlineVerify::getApiSdkUrl();
        $o['account'] = OnlineVerify::getAccount();
        $o['password'] = OnlineVerify::getPassword();
        $o['version'] = OnlineVerify::getVersions();
        $o['waitingTime'] = 180;
        $o['yz_waitingTime'] = 1800;
        $o['hg_waitingTime'] = 600;
        $o['hg_price'] = 30*10000;
        $o['ng_price'] = -1;
        return $o;
    }

    /**
     * 1- 根据支行返回服务窗
     * @desc get/post
     * @param string $branchCode 支行代码
     */
    public function actionBackServWindow($branchCode)
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $client = new Client();
        $r = $client->post(OnlineVerify::getApiServiceUrl() . "/ris/app/getQueueByBranch.app", [
            'branchCode' => $branchCode,
        ], OnlineVerify::getHeaders())->send();
        $c = $r->content;
        echo $c;
        exit;
    }

    /**
     * 2- 根据支行获取问题列表
     * @desc get/post
     * @param string $branchCode 支行代码
     */
    public function actionBackQuestionList($branchCode)
    {
        $client = new Client();
        $r = $client->post(OnlineVerify::getApiServiceUrl() . "/ris/app/getQuestionsByBranch.app", [
            'branchCode' => $branchCode,
        ], OnlineVerify::getHeaders())->send();
        $c = $r->content;
        $c = json_decode($c, true, 512);
        $c['is_update'] = 0;
        return $c;
    }

    /**
     * 3- 人工面签上传当前业务信息 【已废弃】
     * @desc get/post 已废弃
     * @param string $businessId 业务识别号
     * @param string $carPrice 车价
     * @param string $carType 车型
     * @param string $loanAmt 贷款额
     * @param string $loanPeriods 分期期数
     * @param string $customerName 客户名
     * @param string $customerId 客户身份证
     * @param string $idPhoteAddr 身份证照片地址
     * @param string $customerSex 客户性别
     * @param string $birthday 出生日期（主贷人）
     * @param string $fileName 文件名
     * @param string $branchCode 网点代码
     * @param string $branchName 合作机构（网点）
     * @param string $ocrRate 人脸识别（百分比）
     * @param string $location 远程面签时定位信息
     */
    public function actionSubmitBusinessInfo0($businessId, $carPrice, $carType, $loanAmt, $loanPeriods, $customerName, $customerId, $idPhoteAddr, $customerSex, $birthday, $fileName, $branchCode = null, $branchName, $ocrRate, $location)
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $client = new Client();
        $post_data = [
            'businessId' => $businessId,
            'carPrice' => $carPrice,
            'carType' => $carType,
            'loanAmt' => $loanAmt,
            'loanPeriods' => $loanPeriods,
            'customerName' => $customerName,
            'customerId' => $customerId,
            'idPhoteAddr' => $idPhoteAddr,
            'customerSex' => $customerSex,
            'birthday' => $birthday,
            'fileName' => $fileName,
            'branchName' => $branchName,
            'ocrRate' => $ocrRate,
            'location' => $location,
        ];
        if ($branchCode) {
            $post_data['branchCode'] = $branchCode;
        }
        $r = $client->post(OnlineVerify::getApiServiceUrl() . "/ris/app/submitBusinessInfo.app", $post_data, OnlineVerify::getHeaders())->send();
        $c = $r->content;
        echo $c;
        exit;
    }

    /**
     * 3- 人工面签上传当前业务信息
     * @desc get/post
     * @param string $ct_id 征信单号
     * @param string $businessId 业务识别号
     * @param string $location 远程面签时定位信息
     * @param string $ocrRate
     * @param string $commonRepaymentPeoples
     * @throws ErrorException
     * @return string
     */
    public function actionSubmitBusinessInfo($ct_id, $businessId, $location, $ocrRate = 0, $commonRepaymentPeoples = '')
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $ct = OrderCert::findActiveOneById($ct_id);
        if (!$ct) {
            throw new ErrorException("没有找到对应征信！", 100);
        }
        $order = $ct->order;
        if (!$order) {
            throw new ErrorException("没有找到对应订单！", 100);
        }
        $bank = $order->bank;
        if (!$bank) {
            throw new ErrorException("没有找到对应银行！", 100);
        }
        $car = $order->car;
        if (!$car) {
            throw new ErrorException("没有找到对应车辆！", 100);
        }
        $calc = unserialize($car->ot_calculated);
        if (!isset($calc['d_signag_money'])) {
            throw new ErrorException("没有找到申请分期付款总额！", 100);
        }
        if (!$car->ot_car_price || $car->ot_car_price == '0.00') {
            throw new ErrorException("车价不能为空", 100);
        }
        if ($car->ot_car_model == '选择车型'){
            $car->ot_car_model = null;
        }
        if (!$car->ot_car_pose || !$car->ot_car_model) {
            throw new ErrorException("车型不能为空", 100);
        }
        if (!$car->ot_loan_limit || $car->ot_loan_limit == '0.00') {
            throw new ErrorException("申请分期付款总额不能为空", 100);
        }
        if (!$calc['d_signag_money']) {
            throw new ErrorException("申请分期付款总额不能为空", 101);
        }
        $loanAmt = $car->ot_loan_limit;
        if (isset($calc['d_service_money'])) {
            $loanAmt = $loanAmt . " + " . $calc['d_service_money'];
        } else {
            $loanAmt = $loanAmt . " + 0.00";
        }
        if (!$car->ot_loan_year) {
            throw new ErrorException("贷款年限不能为空", 100);
        }
        $ct_attach = unserialize($ct->ct_attach);
        if (!$ct_attach) {
            throw new ErrorException("没有ct_attach！", 100);
        }
        if (!isset($ct_attach['fcard'])) {
            throw new ErrorException("ct_attach没有身份证正面照！", 100);
        } else {
            $fcard = \Yii::$app->params['image_storge_domain'] . $ct_attach['fcard'];
        }
        /*$score_m = Unionpay3002SetLog::find()->where(['id_card' => $ct->ct_code])->andWhere(['or',['status_message' => "业务处理成功"], ['>=', 'score', 70]])->orderBy(['id' => SORT_DESC])->one();
        if (!$score_m) {
            throw new ErrorException("没有找到相似度", 1202);
        } else {
            $score = $score_m->score;
        }*/
        /*$audit = \Yii::$app->runAction('chedai/face-validate/get-face-validate', ['idcard' => $ct->ct_code]);
        if ($audit['data']['audit'] == AliFaceValidate::AUDIT_PASS){
            $score = 700;
        }else{
            $score = -700;
        }*/
        $client = new Client();
        $post_data = [
            'businessId' => $businessId,
            'carPrice' => $car->ot_car_price,
            'carType' => $car->ot_car_pose . '-' . $car->ot_car_model,
//            'loanAmt' => $car->ot_loan_limit,
            'loanAmt' => $loanAmt,
            'loanPeriods' => $car->ot_loan_year,
            'customerName' => $ct->ct_name,
            'customerId' => $ct->ct_code,
            'idPhoteAddr' => $fcard,
            'customerSex' => $ct->ct_sex,
            'birthday' => $ct->ct_birth,
            'fileName' => '',
            'branchName' => $order->o_bank,
            'ocrRate' => $ocrRate,
            'location' => $location,
            'branchCode' => $bank->bk_online_verify_code,
            'commonRepaymentPeoples' => $commonRepaymentPeoples,
        ];
        $r = $client->post(OnlineVerify::getApiServiceUrl() . "/ris/app/submitBusinessInfo.app", $post_data, OnlineVerify::getHeaders())->send();
        $c = $r->content;
        echo $c;
        exit;
    }

    /**
     * 3- 人工面签上传当前业务信息 新
     * @desc get/post
     * @param string $carPrice
     * @param string $businessId
     * @param string $carType
     * @param string $loanAmt
     * @param string $loanPeriods
     * @param string $customerName
     * @param string $customerId
     * @param string $idPhoteAddr
     * @param string $customerSex
     * @param string $birthday
     * @param string $fileName
     * @param string $branchName
     * @param string $ocrRate
     * @param string $location
     * @param string $branchCode
     * @param string $commonRepaymentPeoples
     * @param string $dAgingMoney
     * @param string $dRefund1Month
     * @param string $dRefundMonth
     * @throws
     */
    public function actionSubmitBusinessInfo1($carPrice, $businessId, $carType, $loanAmt, $loanPeriods, $customerName, $customerId, $idPhoteAddr, $customerSex, $birthday, $fileName = '', $branchName, $ocrRate, $location, $branchCode, $commonRepaymentPeoples, $dAgingMoney, $dRefund1Month, $dRefundMonth)
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $client = new Client();
        $post_data = [
            'businessId' => $businessId,
            'carPrice' => $carPrice,
            'carType' => $carType,
            'loanAmt' => $loanAmt,
            'loanPeriods' => $loanPeriods,
            'customerName' => $customerName,
            'customerId' => $customerId,
            'idPhoteAddr' => $idPhoteAddr,
            'customerSex' => $customerSex,
            'birthday' => $birthday,
            'fileName' => $fileName,
            'branchName' => $branchName,
            'ocrRate' => $ocrRate,
            'location' => $location,
            'branchCode' => $branchCode,
            'commonRepaymentPeoples' => $commonRepaymentPeoples,
            'dAgingMoney' => $dAgingMoney,
            'dRefund1Month' => $dRefund1Month,
            'dRefundMonth' => $dRefundMonth,
        ];
        $r = $client->post(OnlineVerify::getApiServiceUrl() . "/ris/app/submitBusinessInfo.app", $post_data, OnlineVerify::getHeaders())->send();
        $c = $r->content;
        echo $c;
        exit;
    }

    /**
     * 4- 自动面签结果提交 【已废弃】
     * @desc get/post 【已废弃】
     * @param string $interviewResult 面签是否通过
     * @param string $businessId 业务识别号
     * @param string $carPrice 车价
     * @param string $carType 车型
     * @param string $loanAmt 贷款额
     * @param string $loanPeriods 分期期数
     * @param string $customerName 客户名
     * @param string $customerId 客户身份证
     * @param string $idPhoteAddr 身份证照片地址
     * @param string $customerSex 客户性别
     * @param string $birthday 出生日期（主贷人）
     * @param string $fileName 文件名
     * @param string $branchCode 网点代码
     * @param string $branchName 合作机构（网点）
     * @param string $ocrRate 人脸识别（百分比）
     * @param string $location 远程面签时定位信息
     */
    public function actionSubmitViewResult0($interviewResult, $businessId, $carPrice, $carType, $loanAmt, $loanPeriods, $customerName, $customerId, $idPhoteAddr, $customerSex, $birthday, $fileName, $branchCode = null, $branchName, $ocrRate, $location)
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $client = new Client();
        $post_data = [
            'interviewResult' => $interviewResult,
            'businessId' => $businessId,
            'carPrice' => $carPrice,
            'carType' => $carType,
            'loanAmt' => $loanAmt,
            'loanPeriods' => $loanPeriods,
            'customerName' => $customerName,
            'customerId' => $customerId,
            'idPhoteAddr' => $idPhoteAddr,
            'customerSex' => $customerSex,
            'birthday' => $birthday,
            'fileName' => $fileName,
            'branchName' => $branchName,
            'ocrRate' => $ocrRate,
            'location' => $location,
        ];
        if ($branchCode) {
            $post_data['branchCode'] = $branchCode;
        }
        $r = $client->post(OnlineVerify::getApiServiceUrl() . "/ris/app/submitInterViewResult.app", $post_data, OnlineVerify::getHeaders())->send();
        $c = $r->content;
        echo $c;
        exit;
    }

    /**
     * 4- 自动面签结果提交
     * @desc get/post
     * @param string $ct_id 征信单号
     * @param string $businessId 业务识别号
     * @param string $location 远程面签时定位信息
     * @param string $interviewResult 面签是否通过
     * @param string $fileName 文件名
     * @param string $deviceModel 面签机型
     * @param string $ocrRate
     * @param string $commonRepaymentPeoples
     * @throws ErrorException
     * @return string
     */
    public function actionSubmitViewResult($ct_id, $interviewResult = '2', $businessId, $location, $fileName, $deviceModel = '', $ocrRate = 0, $commonRepaymentPeoples = '')
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $ct = OrderCert::findActiveOneById($ct_id);
        if (!$ct) {
            throw new ErrorException("没有找到对应征信！", 100);
        }
        $order = $ct->order;
        if (!$order) {
            throw new ErrorException("没有找到对应订单！", 100);
        }
        $bank = $order->bank;
        if (!$bank) {
            throw new ErrorException("没有找到对应银行！", 100);
        }
        $car = $order->car;
        if (!$car) {
            throw new ErrorException("没有找到对应车辆！", 100);
        }
        $calc = unserialize($car->ot_calculated);
        if (!isset($calc['d_signag_money'])) {
            throw new ErrorException("没有找到申请分期付款总额！", 100);
        }
        if (!$car->ot_car_price || $car->ot_car_price == '0.00') {
            throw new ErrorException("车价不能为空", 100);
        }
        if (!$car->ot_car_pose || !$car->ot_car_model) {
            throw new ErrorException("车型不能为空", 100);
        }
        if (!$car->ot_loan_limit || $car->ot_loan_limit == '0.00') {
            throw new ErrorException("申请分期付款总额不能为空", 102);
        }
        if (!$calc['d_signag_money']) {
            throw new ErrorException("申请分期付款总额不能为空", 103);
        }
        $loanAmt = $car->ot_loan_limit;
        if (isset($calc['d_service_money'])) {
            $loanAmt = $loanAmt . " + " . $calc['d_service_money'];
        } else {
            $loanAmt = $loanAmt . " + 0.00";
        }
        if (!$car->ot_loan_year) {
            throw new ErrorException("贷款年限不能为空", 100);
        }
        $ct_attach = unserialize($ct->ct_attach);
        if (!$ct_attach) {
            throw new ErrorException("没有ct_attach！", 100);
        }
        if (!isset($ct_attach['fcard'])) {
            throw new ErrorException("ct_attach没有身份证正面照！", 100);
        } else {
            $fcard = \Yii::$app->params['image_storge_domain'] . $ct_attach['fcard'];
        }
        /*$score_m = Unionpay3002SetLog::find()->where(['id_card' => $ct->ct_code])->andWhere(['>', 'score', 0])->orderBy(['id' => SORT_DESC])->one();
        if (!$score_m) {
//            throw new ErrorException("没有找到相似度", 1202);
            $score = 0;
        } else {
            $score = $score_m->score;
        }*/
        $client = new Client();
        $post_data = [
            'interviewResult' => $interviewResult,
            'businessId' => $businessId,
            'carPrice' => $car->ot_car_price,
            'carType' => $car->ot_car_pose . '-' . $car->ot_car_model,
//            'loanAmt' => $car->ot_loan_limit,
            'loanAmt' => $loanAmt,
            'loanPeriods' => $car->ot_loan_year,
            'customerName' => $ct->ct_name,
            'customerId' => $ct->ct_code,
            'idPhoteAddr' => $fcard,
            'customerSex' => $ct->ct_sex,
            'birthday' => $ct->ct_birth,
            'fileName' => $fileName,
            'branchName' => $order->o_bank,
            'ocrRate' => $ocrRate,
            'location' => $location,
            'branchCode' => $bank->bk_online_verify_code,
            'deviceModel' => $deviceModel,
            'commonRepaymentPeoples' => $commonRepaymentPeoples,
        ];
        /*if ($deviceModel){
            $post_data['deviceModel'] = $deviceModel;
        }*/
        $r = $client->post(OnlineVerify::getApiServiceUrl() . "/ris/app/submitInterViewResult.app", $post_data, OnlineVerify::getHeaders())->send();
        $c = $r->content;
        echo $c;
        exit;
    }

    /**
     * 4- 自动面签结果提交  新
     * @desc get/post
     * @param string $interviewResult
     * @param string $deviceModel
     * @param string $ocrRate
     * @param string $commonRepaymentPeoples
     * @param string $carPrice
     * @param string $businessId
     * @param string $carType
     * @param string $loanAmt
     * @param string $loanPeriods
     * @param string $customerName
     * @param string $customerId
     * @param string $idPhoteAddr
     * @param string $customerSex
     * @param string $birthday
     * @param string $fileName
     * @param string $branchName
     * @param string $location
     * @param string $branchCode
     * @param string $dAgingMoney
     * @param string $dRefund1Month
     * @param string $dRefundMonth
     * @throws
     */
    public function actionSubmitViewResult1($interviewResult = '2', $deviceModel = '', $ocrRate = 0, $commonRepaymentPeoples = '', $carPrice, $businessId, $carType, $loanAmt, $loanPeriods, $customerName, $customerId, $idPhoteAddr, $customerSex, $birthday, $fileName = '', $branchName, $location, $branchCode, $dAgingMoney, $dRefund1Month, $dRefundMonth)
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $client = new Client();
        $post_data = [
        ];
        $post_data = [
            'interviewResult' => $interviewResult,
            'businessId' => $businessId,
            'carPrice' => $carPrice,
            'carType' => $carType,
            'loanAmt' => $loanAmt,
            'loanPeriods' => $loanPeriods,
            'customerName' => $customerName,
            'customerId' => $customerId,
            'idPhoteAddr' => $idPhoteAddr,
            'customerSex' => $customerSex,
            'birthday' => $birthday,
            'fileName' => $fileName,
            'branchName' => $branchName,
            'ocrRate' => $ocrRate,
            'location' => $location,
            'branchCode' => $branchCode,
            'commonRepaymentPeoples' => $commonRepaymentPeoples,
            'dAgingMoney' => $dAgingMoney,
            'dRefund1Month' => $dRefund1Month,
            'dRefundMonth' => $dRefundMonth,
        ];
        /*if ($deviceModel){
            $post_data['deviceModel'] = $deviceModel;
        }*/
        $r = $client->post(OnlineVerify::getApiServiceUrl() . "/ris/app/submitInterViewResult.app", $post_data, OnlineVerify::getHeaders())->send();
        $c = $r->content;
        echo $c;
        exit;
    }

    /**
     * 5- 面签结果状态查询
     * @desc get/post
     * @param string $businessId 业务识别号
     */
    public function actionQueryInterViewResult($businessId, $reuse = 0)
    {
//        \Yii::$app->trigger(\Yii::$app::EVENT_AFTER_ACTION);
        $client = new Client();
        $url = OnlineVerify::getApiServiceUrl() . "/ris/app/queryInterViewResult.app";
        if (Tools::varifyUrl($url)){
            $r = $client->post($url, [
                'businessId' => $businessId,
            ], OnlineVerify::getHeaders())->send();
            $c = $r->content;
            $arr = json_decode($c, true, 512);
            if (isset($arr['RSP_BODY'])){
                if ($reuse) {
                    $c = json_decode($c, true, 512);
                    return $c;
                } else {
                    echo $c;
                    exit;
                }
            }
        }
        $r_map = [
            '0' => '02',
            '1' => '11',
            '2' => '12',
        ];
        $d = \common\models\online_verify\ResInterview::findOne(['BUSINESS_ID' => $businessId]);
        if ($d){
            echo json_encode([
                'RESP_HEAD' => ['Type' => '1111', 'Msg' => '成功'],
                'RSP_BODY' => ['interviewResult' => $r_map[$d->INTERVIEW_RESULT], 'urlResult' => $d->FILE_NAME, 'remark' => $d->REMARK],
            ]);
        }else{
            echo json_encode([
                'RESP_HEAD' => ['Type' => '0000', 'Msg' => '异常'],
                'RSP_BODY' => ['interviewResult' => '01', 'urlResult' => '', 'remark' => ''],
            ]);
        }
        exit;
    }

    /**
     * 验签结果与数据库校验
     * @desc post
     * @param string $ct_id 征信号
     * @throws ErrorException
     */
    public function actionCheckHasResult($ct_id)
    {
        $busi_id = "!ysjr" . str_pad($ct_id, 12, '0', STR_PAD_LEFT);
        $c = $this->actionQueryInterViewResult($busi_id, 1);
        $_c = $c['RSP_BODY'];
        $r = [];
        if ($c['RSP_BODY']['interviewResult'] != OnlineVerify::FACE_SIGN_PASS) {
            switch ($c['RSP_BODY']['interviewResult']) {
                case OnlineVerify::FACE_SIGN_NOT:
                    throw new ErrorException("未面签，请至APP操作远程面签");
                    break;
                case OnlineVerify::VIDEO_UPLOADED:
                    throw new ErrorException("实时面签异常，最近一次远程面签未成功，请至APP操作远程面签");
                    break;
                case OnlineVerify::FACE_SIGN_FAILED:
                    throw new ErrorException("远程面签审核[未通过]:[备注：{$_c['remark']}] [审核人：{$_c['checkUser']}]，请至APP重新操作远程面签");
                    break;
                case OnlineVerify::FACE_SIGN_NO_SURE:
                    throw new ErrorException("远程面签正在审核，请等待");
                    break;
                default:
                    throw new ErrorException("当前状态未定义,联系视频岗");
                    break;
            }
            throw new ErrorException(OnlineVerify::getFaceSignStatus()[$c['RSP_BODY']['interviewResult']] . ", 请等待；如果未面签，请至APP面签");
        } else {
            $cert = OrderCert::findOne(['ct_id' => $ct_id]);
            if (!$cert) {
                throw new ErrorException("没有找到征信");
            }
            $order = $cert->order;
            if (!$order) {
                throw new ErrorException("没有找到订单");
            }
            $car = $order->car;
            $calc = unserialize($car->ot_calculated);
            if (!isset($calc['d_signag_money'])) {
                throw new ErrorException("没有找到申请分期付款总额！", 100);
            }
            if (!$car) {
                throw new ErrorException("没有找到车辆");
            }
            if (!$car->ot_car_price || $car->ot_car_price == '0.00') {
                throw new ErrorException("车价不能为空");
            }
            if ($car->ot_car_model == '选择车型'){
                $car->ot_car_model = null;
            }
            if (!$car->ot_car_pose || !$car->ot_car_model) {
                throw new ErrorException("车型不能为空");
            }
            if (!$car->ot_loan_limit || $car->ot_loan_limit == '0.00') {
                throw new ErrorException("贷款总额不能为空", 100);
            }
            if (!$calc['d_signag_money']) {
                throw new ErrorException("申请分期付款总额不能为空", 104);
            }
            if (!$car->ot_loan_year) {
                throw new ErrorException("贷款年限不能为空");
            }
            /*if ($_c['carPrice']!=$car->ot_car_price){
                $this->actionFaceSignCheck($busi_id, OnlineVerify::FACE_CHECK_FAILED, "车价已修改,请至APP重新远程面签");
                throw new ErrorException("车价已修改,请至APP重新远程面签");
            }*/
            if ($_c['carType']!=$car->ot_car_pose.'-'.$car->ot_car_model){
                $this->actionFaceSignCheck($busi_id, OnlineVerify::FACE_CHECK_FAILED, "车型已修改,请至APP重新远程面签");
                throw new ErrorException("车型已修改,请至APP重新远程面签");
            }
//            Tools::log($_c['loanAmt']);
//            Tools::log($calc['d_signag_money']);
            /*if ($_c['loanAmt']!=$calc['d_signag_money']){
//                $this->actionFaceSignCheck($busi_id, OnlineVerify::FACE_CHECK_FAILED, "申请分期付款总额已修改,请至APP重新远程面签");
//                throw new ErrorException("申请分期付款总额已修改,请至APP重新远程面签");
                throw new ErrorException("申请分期付款总额已修改,请联系技术人员");
            }
            if ($_c['loanPeriods']!=$car->ot_loan_year){
                $this->actionFaceSignCheck($busi_id, OnlineVerify::FACE_CHECK_FAILED, "贷款年限已修改,请至APP重新远程面签");
                throw new ErrorException("贷款年限已修改,请至APP重新远程面签");
            }*/
        }
        $r['req'] = \Yii::$app->request->post();
        $r['plat_back'] = $_c;
        return $r;
    }

    /**
     * 面签审核
     * @desc post/get
     * @param string $businessId
     * @param int $result 0/1
     * @param string $checkUser
     * @param string $remark 备注
     * @throws ErrorException
     * @return mixed|string
     */
    public function actionFaceSignCheck($businessId, $result = OnlineVerify::FACE_CHECK_FAILED, $remark, $checkUser = 'android')
    {
        $client = new Client();
        $r = $client->post(OnlineVerify::getApiServiceUrl() . "/ris/admin/resource/outApprovalResInterview.ajax", [
            'businessId' => $businessId,
            'result' => $result,
            'remark' => $remark,
            'checkUser' => $checkUser,
        ], OnlineVerify::getHeaders())->send();
        $c = $r->content;
        $c = json_decode($c, true, 512);
        return $c;
    }

    /**
     * 获取视频url
     * @desc get/post
     * @param string $businessId
     * @return mixed|string
     */
    public function actionGetVideoUrl($businessId)
    {
        $url = OnlineVerify::getApiServiceUrl() . "/ris/admin/resource/queryInterviewUrl.ajax";
        $client = new Client();
        $r = $client->post($url, [
            'businessId' => $businessId,
        ], OnlineVerify::getHeaders())->send();
        $c = $r->content;
        $c = json_decode($c, true, 512);
        return $c;
    }

    /**
     * 8 - 多条件面签记录查询
     * @param string $branchCode
     * @param string $branchName
     * @param string $businessId
     * @param string $customerName
     * @param string $customerId
     * @param string $interviewResult
     * @param string $locks
     * @param string $userName
     * @param string $interviewUser
     * @param string $carType
     * @param string $carPrice
     * @param string $loanAmt
     * @param string $loanPeriods
     * @return mixed|string
     * @throws
     */
    public function actionGetOvLogs($branchCode, $branchName = '', $businessId = '', $customerName, $customerId, $interviewResult = '', $locks = '', $userName = '', $interviewUser = '', $carType, $carPrice = '', $loanAmt = '', $loanPeriods = '')
    {
        $url = OnlineVerify::getApiServiceUrl() . "/ris/admin/resource/queryInterviewInfo.ajax";
        $client = new Client();
        $r = $client->post($url, [
            'branchCode' => $branchCode,
            'branchName' => $branchName,
            'businessId' => $businessId,
            'customerName' => $customerName,
            'customerId' => $customerId,
            'interviewResult' => $interviewResult,
            'locks' => $locks,
            'userName' => $userName,
            'interviewUser' => $interviewUser,
            'carType' => $carType,
            'carPrice' => $carPrice,
            'loanAmt' => $loanAmt,
            'loanPeriods' => $loanPeriods,
        ], OnlineVerify::getHeaders())->send();
        $c = $r->content;
        $c = json_decode($c, true, 512);
        return $c;
    }

    /**
     * 实时面签绑定新订单
     * @desc get/post
     * @param string $businessId
     * @return mixed|string
     * @throws ErrorException
     */
    public function actionBindNewOrder($businessId)
    {
        $ct_id = str_replace("!ysjr", '', $businessId);
        $cert = OrderCert::findOne(['ct_id'=>$ct_id]);
        if (!$cert){
            throw new ErrorException("没有找到老订单", 7465);
        }
        if ($cert->ct_status==OrderCert::STATUS_OK){
            throw new ErrorException("当前订单没有作废", 6545);
        }
        $order = $cert->order;
        $car = $order->car;
        $id_card = $cert->ct_code;
        $_cert = OrderCert::findOne(['ct_code'=>$id_card, 'ct_key'=>OrderCert::KEY_VIP, 'ct_status'=>OrderCert::STATUS_OK]);
        if (!$_cert){
            throw new ErrorException("没有找到新订单", 54635);
        }
        $_order = $_cert->order;
        $_car = $_order->car;
        if (!$_car){
            throw new ErrorException("新订单没有绑定评估", 54654);
        }
        if ($car->ot_car_type!=$_car->ot_car_type){
            throw new ErrorException("车型不一致", 54156);
        }
        if ($car->ot_car_model!=$_car->ot_car_model){
            throw new ErrorException("车型构成不一致", 4655);
        }
        $_ct_id = $_cert->ct_id;
        $_busi_id = "!ysjr" . str_pad($_ct_id, 12, '0', STR_PAD_LEFT);
        $res_interview = ResInterview::findOne(['BUSINESS_ID'=>$businessId]);
        if (!$res_interview){
            throw new ErrorException("没有找到面签信息", 5465);
        }
        $res_interview->BUSINESS_ID = $_busi_id;
        $res_interview->INTERVIEW_RESULT = '2';
        /*$res_interview->CAR_PRICE = $_car->ot_car_price;
        $calc = unserialize($_car->ot_calculated);
        if (!$calc['d_signag_money']) {
            throw new ErrorException("申请分期付款总额不能为空", 100);
        }
        $loanAmt = $car->ot_loan_limit;
        if (isset($calc['d_service_money'])) {
            $loanAmt = $loanAmt . " + " . $calc['d_service_money'];
        } else {
            $loanAmt = $loanAmt . " + 0.00";
        }
        $res_interview->LOAN_AMT = $loanAmt;*/
//        $res_interview->LOAN_PERIODS =
        if (!$res_interview->save()){
            throw new ErrorException("面前信息保存失败".var_export($res_interview->errors, true));
        }
    }
}